<?php

namespace app\admin\controller;

use app\common\controller\BaseAdminController;

class Dict extends BaseAdminController
{
    /**
     * 获取字典
     * @return \think\response\Json
     */
    public function get_option()
    {
        $key = $this->request->param('key','');
        if(empty($key)){
            return json([]);
        }
        $addons = get_addons_list();
        $root_dir = root_path().'/addons';
        $namespace = 'addons\\';
        foreach ($addons as $value){
            $ndir = $root_dir.'/'.$value.'/app';
            $dirs = get_dir_path(dir: $ndir,type: 'dir');
            $tem_namespace = $namespace.$value.'\\app\\';
            foreach ($dirs as $values){
                $tem_chile_namespace = $tem_namespace.$values.'\\dict\\';
                $tem_dir = $ndir.'/'.$values.'/dict';
                if(is_dir($ndir.'/'.$values.'/dict')){
                    $files = get_dir_path(dir: $tem_dir,type: 'file');
                    foreach ($files as $file){
                        $file_name = str_replace('.php','',$file);
                        $file_name = $tem_chile_namespace.$file_name;
                        $array = (new $file_name())->getOption();
                        if(!empty($array)&& is_array($array)&&$array['key']==$key){
                            if(isset($array['options'])){
                                return json($array['options']);
                            }
                            return json($array);
                        }
                    }

                }
            }
        }
        return json([]);
    }

    /**
     * 获取字典select
     * @return \think\response\Json
     */
    public function get_select()
    {
        $key = $this->request->param('key','');
        if(empty($key)){
            return json([]);
        }
        $addons = get_addons_list();
        $root_dir = root_path().'/addons';
        $namespace = 'addons\\';
        foreach ($addons as $value){
            $ndir = $root_dir.'/'.$value.'/app';
            $dirs = get_dir_path(dir: $ndir,type: 'dir');
            $tem_namespace = $namespace.$value.'\\app\\';
            foreach ($dirs as $values){
                $tem_chile_namespace = $tem_namespace.$values.'\\dict\\';
                $tem_dir = $ndir.'/'.$values.'/dict';
                if(is_dir($ndir.'/'.$values.'/dict')){
                    $files = get_dir_path(dir: $tem_dir,type: 'file');
                    foreach ($files as $file){
                        $file_name = str_replace('.php','',$file);
                        $file_name = $tem_chile_namespace.$file_name;
                        $array = (new $file_name())->getOption();
                        if(!empty($array)&& is_array($array)&&$array['key']==$key){
                            if(isset($array['options'])){
                                $newArray = array_column($array['options'],'label','value');
//                                echo '<pre>';
//                                print_r($newArray);
//                                exit;
                                return json($newArray);
                            }
                            return json($array);
                        }
                    }

                }
            }
        }
        return json([]);
    }
}